<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :modal-append-to-body="false">
    <el-form :rules="rules" ref="dataForm" :model="temp" label-position="right" label-width="20%"
               style='width: 90%; margin-left:1%;'>
        <el-form-item label="父级" prop="parentId" v-if="temp.parentId">
          <span v-if="temp.show" style="color: lightseagreen;">{{this.temp.show}}</span>
          <span v-else style="color: lightseagreen;">{{levStatus}}级&nbsp;&nbsp;{{tData.explainText}}&nbsp;({{tData.fullCodeNo}})</span>
        </el-form-item>
      <el-form-item label="申请全编码" prop="">
        <span>{{applyshow}}</span>
      </el-form-item>
        <el-form-item label="编码" prop="codeNo">
          <el-input @keyup.native="filterNumber" v-model="temp.codeNo"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="explainText">
          <el-input v-model="temp.explainText"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData" id="cfmbtm1">确 定</el-button>
        <el-button v-else type="primary" @click="updateData" id="cfmbtm2">确 定</el-button>
      </div>
  </el-dialog>
</template>
<script>
  import { createOne, deleteOne, deletePost, getOne, rolesAll, updateOneNoId} from '@/api/common'
  import {disableBtn, df, $err, $suc, ERRS, SUCS} from '@/utils'

  export default {
    name: 'formComp',
    props: {
      levStatus: Number,
      temps:Object,
      tData:Object,
      textMap:Object,
    },
    mounted() {
      this.temp=this.temps
    },
    data:function () {
      return {
        dialogFormVisible:false,
        dialogStatus:'',
        temp:{},
        rules: {
          codeNo: [
            {required: true, message: '请输入编码', trigger: 'blur'},
            {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}
          ],
          explainText: [
            {required: true, message: '请输入公司名称', trigger: 'blur'},
            {min: 1, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur'}
          ],
        },
      }
    },
    computed: {
      applyshow: function() {
        //转换undefined
        let r1= this.tData.fullCodeNo>=0?this.tData.fullCodeNo:'';
        let r2= this.temp.codeNo>=0?this.temp.codeNo:'';
        return r1+r2 +this.temp.explainText
      }
    },
    watch:{
      'temp.explainText':function explainText(){
        console.log(this.temp.explainText);
      }
    },
    methods: {
      //过滤数字以外的字符
      filterNumber() {
        this.temp.codeNo = this.temp.codeNo.toString().replace(/[^\d]/g, '')
      },
      //搜索栏 添加子级
      handleCreate() {
        this.temp = {
          codeNo: "",
          explainText: "",
        };
        if (this.levStatus > 0) {
          // return
          this.temp.parentId = this.selectId[this.levStatus - 1]
          this.$parent.getChoseItem()
        }
        if(this.tData.ultimately==true){
          ERRS('备件节点无法添加层级!')
          return;
        }
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      //table操作 添加子级
      handleCreate2(row) {
        this.temp = {
          parentId: row.id,
          codeNo: "",
          explainText: "",
          show: `${row.level}级 ${row.fullname} (${row.fullCodeNo})`
        };
        this.$emit('update:tData', row)
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      //添加提交
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            disableBtn('cfmbtm1');
            if (!this.temp.parentId) {
              delete this.temp.parentId;
            }
            delete this.temp.show;
            createOne('codeExplains', this.temp).then(res => {

              this.dialogFormVisible = false
              // console.log(res);
              if (res.data.code === 0) {
                this.allList(0)
                $suc('创建成功!')
                this.$parent.getList();
              } else {
                ERRS(res.data.message)
              }
            })

          }
        })
      },
      //详情修改
      handleUpdate(row) {
        // console.log(row)
        this.temp = {
          id: row.id,
          codeNo: row.codeNo,
          explainText: row.explainText,
          show: `${row.level}级 ${row.fullname} (${row.fullCodeNo})`
        };
        this.$emit('update:tData', row)
        if (row.parentCodeExplain) //有子级加
          this.temp.parentId = row.parentCodeExplain.id
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      //修改提交
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            disableBtn('cfmbtm2');
            if (!this.temp.parentId) {
              delete this.temp.parentId;
            }
            delete this.temp.show;
            updateOneNoId('codeExplains', this.temp).then(res => {
              this.dialogFormVisible = false
              // console.log(res);
              if (res.data.code === 0) {
                $suc('更新成功!')
                this.$parent.getList();
              } else {
                ERRS(res.data.message)
              }
            })
          }
        })
      },
    }
  }
</script>
